package handler

import (
	"api/basic/global"
	__ "api/basic/proto"
	"api/internal/request"
	"github.com/gin-gonic/gin"
	"net/http"
)

func CreateOrder(c *gin.Context) {
	req := request.CreateOrderReq{}
	err := c.ShouldBindJSON(&req)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	order, err := global.TakeOutClient.CreateOrder(c, &__.CreateOrderReq{
		UserId:     req.UserId,
		GoodsId:    req.GoodsId,
		Quantity:   req.Quantity,
		Address:    req.Address,
		Price:      float32(req.Price),
		Status:     req.Status,
		MerchantId: req.MerchantId,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"data": order})
}
